package edu.jmu.springmvc;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxView;


public class UserListExcelView extends AbstractXlsxView {

	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setHeader("Content-Disposition", "inline;filename="+new String("用户列表".getBytes(),"iso8859-1"));
		@SuppressWarnings("unchecked")
		List<User1> user1List = (List<User1>)model.get("user1List");
		Sheet sheet = workbook.createSheet("users");
		Row header = sheet.createRow(0);
		header.createCell(0).setCellValue("FirstName");
		header.createCell(1).setCellValue("LastName");
		header.createCell(2).setCellValue("日期");
		
		int rowNum = 1;
		for (User1 user1 : user1List) {
			Row row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(user1.getFirstName());
			row.createCell(1).setCellValue(user1.getLastName());
			String createDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
			row.createCell(2).setCellValue(createDate);
		}
		
	}

}
